#include <stdio.h>
#include <string.h>
#include "arch.h"

void showHelp();

int main(int argc, char *argv[])
{	
	Arch arch;
 
 	int Error=0;

	if(argc<2)
	{
		showHelp();
		return 0;
	}
	
	int optionPos=3;
	std::string fileIn, fileOut;
	if(argv[2][0]=='-' && argc==3)
	{	
		optionPos=2;
		fileOut="out.arch";	
	}
	else
		fileOut=argv[2];
	fileIn=argv[1];
	int smartReset=0;
	if(argc>4)
	
		if(argv[4][1]=='s')
			smartReset=argv[4][2]-48;
	printf("\n");
	if(!strcmp(argv[optionPos],"-cl"))
	{
		printf("LZW compression started");	
		if(arch.compress(fileIn, fileOut,'l',smartReset)==0)
			Error=1;
	}
	else if(!strcmp(argv[optionPos],"-ul"))
	{
		printf("LZW decompression started");	
		if(arch.decompress(fileIn, fileOut,'l',smartReset)==0)
			Error=1;
	}
	else if(!strcmp(argv[optionPos],"-ch"))
	{
		printf("Huffman compression started");	
		if(arch.compress(fileIn, fileOut,'h',smartReset)==0)
			Error=1;
	}
	else if(!strcmp(argv[optionPos],"-uh"))
	{
		printf("Huffman decompression started");	
		if(arch.decompress(fileIn, fileOut,'h',smartReset)==0)
			Error=1;
	}
	else
		Error=1;
	
	if(Error)
		showHelp();
	else
		printf("\nSuccessfully ened\n");
	return 0;
}

void showHelp()
{
	printf("\narch\t\t User Commands\n"
	"SYNOPSIS\n\t./arch incoming FILE [output FILE] [OPTION]\n\t(!) [output FILE] will be rewritten\n\n"
	"OPTIONS:\n\t -cl\n\t\t LZW code\n\t -ul\n\t\t LZW decode\n\t -ch\n\t\t Huffman code\n\t -uh\n\t\t Huffman decode\n");
}
